package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductGetInfoByCatalogIdReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductGetInfoByCatalogIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ShoppingcartProductGetInfoByCatalogId {
	
	private static ShoppingcartProductGetInfoByCatalogIdCall shoppingcartProductGetInfoByCatalogIdCall = new ShoppingcartProductGetInfoByCatalogIdCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据规格id获取购物车商品信息");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductGetInfoByCatalogIdCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_001() throws Exception {
		Logger.start(true,"根据catalogid获取购物车商品信息（默认source为0）");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(userId);
			req.setCatalogId(String.valueOf(productDB.get("catalogid")));
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject o = ret.getJSONObject("data");
			ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);

			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_002() throws Exception {
		Logger.start(true,"输入错误的catalogid获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(userId);
			req.setCatalogId("1234213asd");
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String data = ret.getString("data");

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals("null", data, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_003() throws Exception {
		Logger.start(true,"输入错误的userid获取购物车商品信息");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(142131);
			req.setCatalogId(String.valueOf(productDB.get("catalogid")));
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String data = ret.getString("data");

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals("null", data, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_004() throws Exception {
		Logger.start(true,"根据catalogid和source获取购物车商品信息");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(userId);
			req.setSource(1);
			req.setCatalogId(String.valueOf(productDB.get("catalogid")));
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject o = ret.getJSONObject("data");
			ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);

			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_005() throws Exception {
		Logger.start(true,"输入错误的source获取购物车商品信息");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(userId);
			req.setSource(0);
			req.setCatalogId(String.valueOf(productDB.get("catalogid")));
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String data = ret.getString("data");

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals("null", data, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_006() throws Exception {
		Logger.start(true,"不传入userid");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(0);
			req.setSource(0);
			req.setCatalogId(String.valueOf(productDB.get("catalogid")));
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:错误的userId;", message, "不传入userid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_007() throws Exception {
		Logger.start(true,"不传入catalogid");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoByCatalogIdReq req = new ShoppingcartProductGetInfoByCatalogIdReq();
			
			req.setUserId(userId);
			req.setSource(0);
			req.setCatalogId("");
			
			JSONObject ret = shoppingcartProductGetInfoByCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("catalogId:catalogId不能为空;", message, "不传入catalogid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
